﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace CTManagerSoln
{
    public partial class frmUserCreateAccount : Form
    {
        User us;
        bool isUpdate;
        public frmUserCreateAccount(bool Update)
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.TopMost = true;
            us = new User();
            isUpdate = Update;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string Err = string.Empty;
            string userName=txtUserName.Text;
            string pass=txtPass.Text;
            string passConfirm=txtConfirmPass.Text;
            string email=txtEmail.Text;
            string fullname = txtfullName.Text;
            string _datebirthday = datebirthday.Text;
            string position = txtposition.Text;
            

            if (string.IsNullOrEmpty(userName))
            {
                Err += "- User Name chưa nhâp. \n";
            }
                     
            if (string.IsNullOrEmpty(pass))
            {
                Err += "- Mật khẩu chưa nhập. \n";
            }
            if (string.IsNullOrEmpty(passConfirm))
            {
                Err += "- Nhập lại mật khẩu. \n";
            }
            if (string.IsNullOrEmpty(email))
            {
                Err += "- Email chưa nhập. \n";
            }
            else
            {
                try
                {
                    var ab = new MailAddress(email);
                }
                catch
                {
                    Err += "- Email chưa hợp lệ. \n";
                }
            }
            
            if (Err.Length<=0)
            {
                if (pass.ToUpper()==passConfirm.ToUpper())
                {
                    //User.Insert(userName, pass, email, fullname, _datebirthday, position, 0, 0);
                    User.inSerts(userName, pass, email, fullname, _datebirthday, position, 0, 0);
                }
                else
                {
                    MessageBox.Show("mật khẩu chưa khớp.");
                }
            }
            else
            {
                MessageBox.Show(Err);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmUserCreateAccount_Load(object sender, EventArgs e)
        {
            if (isUpdate)
            {
                txtUserName.Enabled = !isUpdate;
                txtConfirmPass.Text = "";
                txtConfirmPass.Enabled = !isUpdate;
                txtEmail.Text = "***";
                txtEmail.Enabled = !isUpdate;
            }
        }
    }
}
